﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace MyMusic
{
    public partial class GamePage
    {
        /// <summary>
        /// 程序启动时间加载全部内容
        /// </summary>
        private void LoadAllResource()
        {
            Cycles cs;
            Vector2 coord;//角色所在位置
            Texture2D img = contentManager.Load<Texture2D>("img");
            Cycle c;

            cs = new Cycles();
            coord = new Vector2(35, 40);
            c = new Cycle()
            {
                Corpse = img,
                ISDisposed1 = false,
                Postion = coord
            };
            cs.cycle = c;
            cs.msec = 0;
            cs.Note = 1;
            list.Add(cs);

            cs = new Cycles();
            coord = new Vector2(95, 40);
            c = new Cycle()
            {
                Corpse = img,
                ISDisposed1 = false,
                Postion = coord
            };
            cs.cycle = c;
            cs.msec = 1000;
            cs.Note = 2;
            list.Add(cs);

            cs = new Cycles();
            coord = new Vector2(155, 40);
            c = new Cycle()
            {
                Corpse = img,
                ISDisposed1 = false,
                Postion = coord
            };
            cs.cycle = c;
            cs.msec = 2000;
            cs.Note = 3;
            list.Add(cs);

            cs = new Cycles();
            coord = new Vector2(215, 40);
            c = new Cycle()
            {
                Corpse = img,
                ISDisposed1 = false,
                Postion = coord
            };
            cs.cycle = c;
            cs.msec = 3000;
            cs.Note = 4;
            list.Add(cs);

            cs = new Cycles();
            coord = new Vector2(275, 40);
            c = new Cycle()
            {
                Corpse = img,
                ISDisposed1 = false,
                Postion = coord
            };
            cs.cycle = c;
            cs.msec = 4000;
            cs.Note = 5;
            list.Add(cs);

            cs = new Cycles();
            coord = new Vector2(335, 40);
            c = new Cycle()
            {
                Corpse = img,
                ISDisposed1 = false,
                Postion = coord
            };
            cs.cycle = c;
            cs.msec = 5000;
            cs.Note = 6;
            list.Add(cs);

            cs = new Cycles();
            coord = new Vector2(395, 40);
            c = new Cycle()
            {
                Corpse = img,
                ISDisposed1 = false,
                Postion = coord
            };
            cs.cycle = c;
            cs.msec = 6000;
            cs.Note = 7;
            list.Add(cs);
        }

        /// <summary>
        /// 更新资源
        /// </summary>
        private void UPLoadResource(double msec)
        {
            var m = list.FirstOrDefault(p => p.msec <= msec);
            if (m != null)
            {
                listNote.Add(m.cycle);
                list.Remove(m);
            }
        }

        /// <summary>
        /// 判断点击
        /// </summary>
        private void IsClickSuccess()
        {
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                if (gesture.GestureType == GestureType.Tap)
                {
                    var yf = GetClickPosition(gesture.Position);
                    if (yf == 0)
                    {
                        break;
                    }
                    switch (yf)
                    {
                        case 1:
                            if (IsNoteSuccess(1))
                                Dao.Play();
                            break;
                        case 2:
                            if (IsNoteSuccess(2))
                                Ruai.Play();
                            break;
                        case 3:
                            if (IsNoteSuccess(3))
                                Mi.Play();
                            break;
                        case 4:
                            if (IsNoteSuccess(4))
                                Fa.Play();
                            break;
                        case 5:
                            if (IsNoteSuccess(5))
                                Shao.Play();
                            break;
                        case 6:
                            if (IsNoteSuccess(6))
                                La.Play();
                            break;
                        case 7:
                            if (IsNoteSuccess(7))
                                Xi.Play();
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// 获得点击所对应的音符
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        private int GetClickPosition(Vector2 pos)
        {
            if (pos.Y < 650)
            {
                return 0;
            }
            var x = pos.X;
            if (x > 0 && x <= 90)
            {
                return 1;
            }
            if (x > 90 && x <= 150)
            {
                return 2;
            }
            if (x > 150 && x <= 210)
            {
                return 3;
            }
            if (x > 210 && x <= 270)
            {
                return 4;
            }
            if (x > 270 && x <= 330)
            {
                return 5;
            }
            if (x > 330 && x <= 390)
            {
                return 6;
            }
            if (x > 390 && x <= 480)
            {
                return 7;
            }
            return 0;
        }

        /// <summary>
        /// 在点击部位是否有音符
        /// </summary>
        /// <param name="Note"></param>
        /// <returns></returns>
        private bool IsNoteSuccess(int Note)
        {
            var m = listNote.FirstOrDefault(p => p.Postion.Y > 650);

            if (m != null)
            {
                listNote.Remove(m);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 更新图片位置
        /// </summary>
        private void Update()
        {
            for (int i = 0; i < listNote.Count; i++)
            {
                var m = listNote[i];
                m.Postion = new Vector2(m.Postion.X, m.Postion.Y + speed);
                if (m.Postion.Y > 780)
                {
                    listNote.Remove(m);
                }
            }
        }

        /// <summary>
        /// 绘制图形
        /// </summary>
        private void Draw()
        {
            foreach (Cycle m in listNote)
            {

                //    spriteBatch.Draw(
                //        m.Corpse,//要绘制的精灵
                //        m.Postion,//绘制的位置
                //        new Rectangle(0, 0, 48, 20),//绘制精灵的哪一部分
                //        Color.White, //颜色
                //        0, //旋转角度
                //        Vector2.Zero,//旋转参考点 
                //        1, //缩放倍数
                //        SpriteEffects.None, //翻转
                //        0 //层深
                //        );
                spriteBatch.Draw(m.Corpse, m.Postion, Color.White);
            }
        }
    }
}
